﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;
using Jungle.Code.DAL;

namespace Jungle.Code.Core
{
	public class Module
	{
		#region Fields
		private int _moduleID;
		private string _name;
		private string _basePath;
		private string _userControlFile;
		private string _adminControlFile;
		private int _group;
		#endregion

		#region Properties
		public int ModuleID {
			get {
				return _moduleID;
			}
			set {
				_moduleID = value;
			}
		}
		public string Name {
			get {
				return _name;
			}
			set {
				_name = value;
			}
		}
		public string BasePath {
			get {
				return _basePath;
			}
			set {
				_basePath = value;
			}
		}
		public string UserControlFile {
			get {
				return _userControlFile;
			}
			set {
				_userControlFile = value;
			}
		}
		public string AdminControlFile {
			get {
				return _adminControlFile;
			}
			set {
				_adminControlFile = value;
			}
		}
		public int Group {
			get {
				return _group;
			}
			set {
				_group = value;
			}
		}
		#endregion

		public static Module GetByModuleID(int moduleID) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crModules_GetByModuleID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("moduleID", SqlDbType.Int).Value = moduleID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					Module module = new Module();
					dr.Read();
					SetAllFields(module, dr);
					dr.Close();
					return module;
				}
				dr.Close();
			}
			return null;
		}

		public static List<Module> GetAll() {
			List<Module> modules = new List<Module>();
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crModules_GetAll", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					while (dr.Read()) {
						Module module = new Module();
						SetAllFields(module, dr);
						modules.Add(module);
					}
				}
				dr.Close();
			}
			return modules;
		}


		private static void SetAllFields(Module module, SqlDataReader dr) {
			module._moduleID = (int)dr["moduleID"];
			module._name = (string)dr["name"];
			module._basePath = (string)dr["basePath"];
			module._userControlFile = (string)dr["userControlFile"];
			module._adminControlFile = (string)dr["adminControlFile"];
			module._group = (int)dr["group"];
		}


	}
}
